package com.tmtc.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.tmtc.annotation.SystemControllerLog;
import com.tmtc.constant.MessageVar;
import com.tmtc.constant.ParameterConstant;
import com.tmtc.frame.JsonResult;
import com.tmtc.frame.PageResult;
import com.tmtc.frame.ServiceException;
import com.tmtc.po.AdPic;
import com.tmtc.po.AdPicRepository;
import com.tmtc.service.AdPicService;
import com.tmtc.utils.IdWorker;
import com.tmtc.utils.VerificationUtil;

@Controller
@RequestMapping("/adPic")
public class AdPicController {
	@Autowired
	AdPicService adPicService;

	/**
	 * 添加和修改广告
	 */
	@RequestMapping("/{edit}")
	@ResponseBody
	@SystemControllerLog("广告-编辑")
	public JsonResult edit(HttpServletRequest request, HttpServletResponse response, @PathVariable String edit,
			@Valid @ModelAttribute AdPic adPic, @RequestParam CommonsMultipartFile[] picFile) throws ServiceException {
		if (edit.equals("add")) {
			if (0 == VerificationUtil.length(adPic.getCompany_id())) {
				adPic.setCompany_id(null);
			}
			adPic.setId(IdWorker.nextId());
			adPic.setIs_check(ParameterConstant.IS_CHECK);
			adPic.setCreate_time(new Date());
			adPicService.insert(adPic, picFile, request);
			return new JsonResult();
		} else if (edit.equals("update")) {
			if (0 == VerificationUtil.length(adPic.getCompany_id())) {
				adPic.setCompany_id(null);
			}
			adPicService.update(adPic, picFile, request);
			return new JsonResult();
		}
		return new JsonResult(0, MessageVar.DO_ERROR, null);
	}

	/**
	 * 删除广告
	 */
	@RequestMapping("/delete")
	@ResponseBody
	@SystemControllerLog("广告-删除")
	public JsonResult delete(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = true) String ids) {
		AdPic adPic = new AdPic();
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			adPic.setId(id);
			adPic.setIs_check(ParameterConstant.IS_NOT_CHECK);
			adPicService.update(adPic);
			if (0 == adPicService.update(adPic)) {
				return new JsonResult(0, MessageVar.DELETE_ERROR, null);
			}
		}
		return new JsonResult(0, MessageVar.DELETE_SUCCESS, null);
	}

	/**
	 * 永久删除广告
	 */
	@RequestMapping("/forEver")
	@ResponseBody
	@SystemControllerLog("广告-永久删除")
	public JsonResult forEver(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = true) String ids) {
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			if (0 == adPicService.delete(id)) {
				return new JsonResult(0, MessageVar.DELETE_ERROR, 0);
			}
		}
		return new JsonResult(1, MessageVar.DELETE_SUCCESS, null);
	}

	/**
	 * 查询所有广告
	 */
	@RequestMapping("/query")
	@ResponseBody
	@SystemControllerLog("广告-查询")
	public PageResult query(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = true) Integer page, @RequestParam(required = true) Integer rows,
			@RequestParam(required = false) String title, @RequestParam(required = false) String company_id) {
		if (null != page && null != rows) {
			AdPicRepository adPicRepository = new AdPicRepository();
			AdPicRepository.Criteria criteria = adPicRepository.createCriteria();
			adPicRepository.setPageSize(rows);
			adPicRepository.setRowIndex((page - 1) * rows);
			if (null != title && "" != title) {
				criteria.andTitleLike("%" + title + "%");
			}
			if (null != company_id) {
				criteria.andCompany_idEqualTo(company_id);
			}
			criteria.andIs_checkEqualTo(ParameterConstant.IS_CHECK);
			PageResult pageResult = adPicService.selectByPage(adPicRepository);
			return pageResult;
		}
		return new PageResult(0, null, 0);
	}

	/**
	 * 查看广告图片
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	@SystemControllerLog("广告-查看图片")
	public JsonResult selectById(HttpServletRequest request, HttpServletResponse response, @RequestParam String id) {
		List<String> list = adPicService.selectPic(id);
		return new JsonResult(1, MessageVar.ADPIC_SELECT_SUCCESS, list);
	}

	/**
	 * 恢复使用
	 * 
	 * @param request
	 * @param response
	 * @param id
	 *            产品ID
	 * @return
	 */
	@RequestMapping("/updateRecover")
	@ResponseBody
	@SystemControllerLog("广告-恢复使用")
	public JsonResult updateRecover(HttpServletRequest request, HttpServletResponse response,
			@RequestParam String ids) {
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			if (0 == adPicService.updateRecover(id)) {
				return new JsonResult(0, MessageVar.ISCHECK_ERROR, 0);
			}
		}
		return new JsonResult(1, MessageVar.ISCHECK_SUCCESS, 1);
	}
}
